import * as TYPES from "../action-types";
import { getArtistList } from "services/artist.js";

let actions = {
  setCurrentArea: (data) => {
    return {
      type: TYPES.ART_CURRENT_AREA,
      payload: data,
    };
  },
  setCurrentType: (data) => {
    return async (dispatch, getState) => {
      const currentType = getState().getIn(["artist", "currentType"]);
      if (data.type === currentType.type) return;
      await dispatch({
        type: TYPES.ART_CURRENT_TYPE,
        payload: data,
      });
      await dispatch(actions.getArtistList());
    };
  },
  getArtistList: (alpha) => {
    return async (dispatch, getState) => {
      const currentArea = getState().getIn(["artist", "currentArea"]);
      const currentType = getState().getIn(["artist", "currentType"]);
      const res = await getArtistList(currentArea, currentType.type, alpha);
      dispatch({
        type: TYPES.ART_CURRENT_LIST,
        payload: res.artists,
      });
    };
  },
};

export default actions;
